<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop:'timeOfEntry', order: 'ascending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ hireTypeFormat(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 4, 20, 30]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>

      <!-- 新增弹层 -->
      <el-dialog title="新增员工" :visible.sync="showDialog" @close="hClose">
        <empDialog ref="empDialog" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployee, delEmployee } from '@/api/employees'
import employeeEnum from '@/constant/employees'
import empDialog from './empDialog.vue'
// console.log('employeeEnum', employeeEnum.hireType)
// 方法二 [{id:1,value:'正式'},{id:2,value:'非正式'}] ==> {1:'正式',2:'非正式'}
// const hireType = {}
// employeeEnum.hireType.forEach(item => {
//   hireType[item.id] = item.value
// })

// 方法三:reduce
const hireType = employeeEnum.hireType.reduce((arr, item) => {
  arr[item.id] = item.value
  return arr
}, {})
export default {
  components: {
    empDialog
  },
  data() {
    return {
      showDialog: false, // 控制 新增弹层
      list: [], // 保存员工列表
      page: 1, // 页码
      size: 2, // 每页条数
      total: 0 // 员工总数
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    // 删除
    hDel(id) {
      this.$confirm('是否删除该员工?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delEmployee(id)
        console.log(res, '删除')
        // 处理删除的特殊情况
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        this.loadEmployee()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 重新校验表单
    hClose() {
      this.$refs.empDialog.resetForm()
    },
    hSuccess() {
      // 获取最大页码值
      this.total++
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
      this.loadEmployee()
      this.showDialog = false
    },
    // 分页
    handleSizeChange(val) {
      // val 每页显示的条数
      console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadEmployee()
    },
    handleCurrentChange(val) {
      // val是页码值
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployee()
    },
    // 枚举 返回的是正式和非正式
    hireTypeFormat(data) {
      // 方法一:
      // data ==> 1 或者 2
      // return EmployeeEnum.hireType.find(item => item.id === data).value || '未知'
      return hireType[data] || '未知'
    },
    // 获取员工列表
    async loadEmployee() {
      try {
        const res = await getEmployee(this.page, this.size)
        console.log(res, '获取员工列表')
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error, 'error')
      }
    }
  }
}
</script>
